package com.softlab.retailwholesalesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.FlexEntity;
import com.softlab.retailwholesalesystem.entity.GoodsEntity;
import com.softlab.retailwholesalesystem.entity.OrderEntity;
import com.softlab.retailwholesalesystem.entity.OrderGoodsMapEntity;
import com.softlab.retailwholesalesystem.entity.VO.QuickEntity;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import com.softlab.retailwholesalesystem.service.IOrderGoodsMapService;
import com.softlab.retailwholesalesystem.service.IOrderService;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/quick")
@CrossOrigin
public class QuickOrderController extends BaseController{
    @Autowired
    private IOrderService Service;

    @Autowired
    private IOrderGoodsMapService orderGoodsMapService;

    @Autowired
    private IGoodsService goodsService;

    @GetMapping
    public JsonResult<List<OrderEntity>> getAll(){
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("order_type","Q");
        List<OrderEntity> orderEntities = Service.list(wrapper);
        /*不该在这里出现，偷个懒*/
        for (OrderEntity o:
                orderEntities) {
            Service.setProfitAndTotalByType(o);
        }
        orderEntities = Service.list(wrapper);
        return new JsonResult<>(200,"快速订单列表查询成功", orderEntities);
    }



    @PostMapping("/add")
    public JsonResult<Long> addOrder(@RequestBody FlexEntity flex){
        Long id = Service.newQuick(flex.getUsername());
        return new JsonResult<>(200, "订单增加成功!", id);
    }

    @PutMapping
    public JsonResult<Void> changeOrder(@RequestBody OrderEntity order){
        Service.setProfitAndTotalByType(order);
//        if (order.getStatus().equals("待付款"))
//            Service.confirm(order);
        Service.updateOrder(order);
        return new JsonResult<>(200, "订单修改成功");
    }

    @DeleteMapping("{id}")
    public JsonResult<Void> deleteOrder(@PathVariable Long id){
        OrderEntity order=new OrderEntity();
        order.setOrderId(id);
        order.setIsDelete(1);
        Service.updateById(order);
        return new JsonResult<>(200, "订单删除成功");
    }

    @GetMapping("/search")
    public JsonResult<List<OrderEntity>> getOrderList(OrderEntity order){
        order.setOrderType("Q");
        List<OrderEntity> list = Service.searchOrders(order);
        /*不该在这里出现，偷个懒*/
        for (OrderEntity o:
                list) {
            Service.setProfitAndTotalByType(o);
        }
        list = Service.searchOrders(order);

        return new JsonResult<>(200, "订单查找成功", list);
    }

    @PostMapping("/hands")
    public JsonResult<Long> addQuickGoods(@RequestBody OrderGoodsMapEntity entity){
        Long id = Service.addQuickGoods(entity);
        return new JsonResult<>(200, "货品添加成功", id);
    }

    @DeleteMapping("/hands/{id}")
    public JsonResult<Void> delQuickGoods(@PathVariable Long id){
        Service.delQuickGoods(id);
        return new JsonResult<>(200, "货品删除成功");
    }

    @GetMapping("{id}")
    public JsonResult<QuickEntity> getSpecific(@PathVariable Long id){
        OrderEntity order = new OrderEntity();
        order.setOrderId(id);
        order.setOrderType("Q");
        OrderEntity entity = Service.searchOrders(order).get(0);



        QuickEntity quickEntity = new QuickEntity();
        quickEntity.setOrder(entity);

        OrderGoodsMapEntity orderGoodsMapEntity = new OrderGoodsMapEntity();
        orderGoodsMapEntity.setOrderId(id);
        List<OrderGoodsMapEntity> map = orderGoodsMapService.getMap(orderGoodsMapEntity);


        quickEntity.setMap(map);

        return new JsonResult<>(200,"订单查找成功", quickEntity);
    }

}
